package com.ejoysoft.servlet;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Vector;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.ejoysoft.common.Globa;
import com.ejoysoft.wlanyz.business.Ap;
import com.ejoysoft.wlanyz.business.Task;
public class TaskInfoServlet extends HttpServlet implements Servlet
{
	
	private static final long serialVersionUID = 217251451801586160L;
	public TaskInfoServlet()
	{
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException {	// TODO Auto-generated method stub
		try {
			
		this.execute(req, resp);
		}catch(Exception e) {
			e.printStackTrace();
			throw new IOException(e.getMessage());
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		System.err.println("12");
		this.doGet(req, resp);
	}

	private void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		 Globa globa = new Globa(); 
		 String content1 =  req.getParameter("TaskPhone");
		 String where=" where 1=1 ";
		 if(content1!=null)
		 {
			 where+=" and strTaskPhone = '"+content1+"'";
		 }
		 
		 StringBuffer sbReturn = new StringBuffer("<?xml version='1.0' encoding='utf-8'?> ");
			req.setCharacterEncoding("utf-8");		
			resp.setCharacterEncoding("utf-8");	
			try
			{
				
				Task task = new Task(globa,false);
					Vector<Task> vctAd = new Vector<Task>();
					vctAd = task.list(where,0,0);
					sbReturn.append("<Root>");
					if (vctAd!=null && vctAd.size() > 0)
					{
						for (int i = 0; i < vctAd.size(); i++)
						{
							task = vctAd.get(i);
							if (task!=null)
							{   
								
										        sbReturn.append(returnTask(task));
							}
						}
					}
					sbReturn.append("</Root>");
				

			} catch (Exception e)
			{
				e.printStackTrace();
			} finally
			{
				// 关闭数据库连接对象
				globa.closeCon();
				System.out.println(sbReturn.toString());
				resp.getWriter().println(sbReturn.toString());
			}
 
	  }
	


	private StringBuffer returnTask (Task task )
	{   
		Globa globa = new Globa();
		
				StringBuffer sbReturnContent = new StringBuffer();
				if(task!=null)
				{
					String id=(task.getStrTaskId()==null?"":task.getStrTaskId());
					String manString=(task.getStrTaskMan()==null?"": task.getStrTaskMan());
					String phoneString=(task.getStrTaskPhone()==null?"": task.getStrTaskPhone());
					String nameString=(task.getStrTaskName()==null?"": task.getStrTaskName());
					String tasktypeString=(task.getStrTaskType()==null?"": task.getStrTaskType()) ;
					 String nettypeString=(task.getStrNetType()==null?"": task.getStrNetType());
					 String desString=(task.getStrTaskDes()==null?"": task.getStrTaskDes()) ;
					 String pingString=(task.getStrPingAddr()==null?"": task.getStrPingAddr());
					 String http=(task.getStrHttpSite()==null?"": task.getStrHttpSite());
					 String ftpString=(task.getStrFtpAddress()==null?"": task.getStrFtpAddress());
					sbReturnContent.append("<Task>");
					sbReturnContent.append("<TaskId>" + id + "</TaskId>");
					sbReturnContent.append("<TaskMan>" + manString + "</TaskMan>");
					sbReturnContent.append("<TaskPhone>" + phoneString + "</TaskPhone>");
					sbReturnContent.append("<TaskName>" + nameString + "</TaskName>");
//					sbReturnContent.append("<APName>" + obj1==null?"":obj1.getDevice_Name() + "</APName>");
					sbReturnContent.append("<TaskType>" + tasktypeString + "</TaskType>");
					sbReturnContent.append("<NetworkType>" +nettypeString + "</NetworkType>");
					sbReturnContent.append("<TaskDes>" +desString + "</TaskDes>");
					sbReturnContent.append("<PingAddress>" + pingString + "</PingAddress>");
					sbReturnContent.append("<HttpSite>" + http + "</HttpSite>");
					sbReturnContent.append("<FTPAddress>" +ftpString+ "</FTPAddress>");
					sbReturnContent.append("<TestCount>" + task.getStrTestCount()+ "</TestCount>");
					sbReturnContent.append("<ApCount>" +" "+"</ApCount>");
					sbReturnContent.append("<MacCount>" +" "+"</MacCount>");
					sbReturnContent.append("<TDGSMCount>"+" "+"</TDGSMCount>");
					String apids=task.getStrApIds();
					if(apids!=null){
						 String str[] = apids.split(",");
					        if(str!=null){
					 			  for (int j = 0; j < str.length; j++) {
									String temp = str[j];
									String Where= " where device_Id = '" + temp + "'";
									Ap objAp=new Ap(globa);
									Ap obj1=objAp.show2(Where);
									sbReturnContent.append("<Apinfo>");
									if(obj1!=null){
										String deString=(obj1.getDevice_Name()==null?"":obj1.getDevice_Name());
										String macString=(obj1.getStrMac_Addr()==null?"":obj1.getStrMac_Addr());
										sbReturnContent.append("<ApName>" +deString+"</ApName>");
										sbReturnContent.append("<ApMac>"+macString+"</ApMac>");	
									}
									sbReturnContent.append("</Apinfo>");
									
					 			  }
					        }
					}
					sbReturnContent.append("</Task>");	
				}
				globa.closeCon();
				return sbReturnContent;
			  }
				
				
 		 
		
	}
	
